package com.yspay.sample;

import java.io.InputStream;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import com.yspay.sample.config.DirectPayConfig;
import com.yspay.sample.sign.SignUtils;

public class ApipaySubmit {
	/**
	 * api请求的签名工具方法，把请求参数按照字符排序拼接，然后进行RAS加密
	 * 
	 * @param request
	 * @param sParaTemp
	 * @return
	 * @see
	 */
	public static String sign(HttpServletRequest request,
			Map<String, String> sParaTemp, String partnerCert,
			String partnerPassword) {
		Map<String, String> sPara = SignUtils.paraFilter(sParaTemp);

		ServletContext servletContext = request.getSession()
				.getServletContext();

		String partnerId = sParaTemp.get("partner_id");
		if (partnerId.endsWith("wuzl05")) {
			partnerCert = "/WEB-INF/cert/wuzl05.pfx";
		}
		InputStream pfxCertFileInputStream = servletContext
				.getResourceAsStream(partnerCert);

		String signResult = "";
		try {
			signResult = SignUtils.rsaSign(sPara, sParaTemp.get("charset"),
					pfxCertFileInputStream, partnerPassword);
		} catch (Exception e) {
			throw new RuntimeException("签名失败，请检查证书文件是否存在，密码是否正确");
		}

		return signResult;
	}

	/**
	 * API验证签名工具，把签名值，请求字符编码，返回结果(json body)传递过来进行验证签名
	 * 
	 * @param request
	 * @param sign
	 * @param responseBody
	 * @param charset
	 * @return
	 * @see
	 */
	public static boolean verifySign(HttpServletRequest request, String sign,
			String responseBody, String charset, String certPathPublic) {
		ServletContext servletContext = request.getSession()
				.getServletContext();
		InputStream publicCertFileInputStream = servletContext
				.getResourceAsStream(certPathPublic);

		boolean isSign = false;
		try {
			isSign = SignUtils.rsaCheckContent(publicCertFileInputStream,
					responseBody, sign, charset);
		} catch (Exception e) {
			throw new RuntimeException("验证签名失败，请检查银盛公钥证书文件是否存在");
		}
		return isSign;
	}

	public static Map<String, String> buildRequestPara(
			HttpServletRequest request, Map<String, String> sParaTemp,
			String partnerCert, String partnerPassword) {
		Map<String, String> sPara = SignUtils.paraFilter(sParaTemp);

		ServletContext servletContext = request.getSession()
				.getServletContext();

		String partnerId = sParaTemp.get("partner_id");

		if (partnerId.endsWith("wuzl05")) {
			partnerCert = "/WEB-INF/cert/wuzl05.pfx";
		}
		InputStream pfxCertFileInputStream = servletContext
				.getResourceAsStream(partnerCert);

		String mysign = "";
		try {
			mysign = SignUtils.rsaSign(sPara, sParaTemp.get("charset"),
					pfxCertFileInputStream, partnerPassword);
		} catch (Exception e) {
			throw new RuntimeException("签名失败，请检查证书文件是否存在，密码是否正确");
		}

		sPara.put("sign", mysign);

		return sPara;
	}

	public static boolean verifySign(HttpServletRequest request,
			Map<String, String> params, String certPathPublic) {
		ServletContext servletContext = request.getSession()
				.getServletContext();
		InputStream publicCertFileInputStream = servletContext
				.getResourceAsStream(certPathPublic);
		String sign = "";
		if (params.get("sign") != null) {
			sign = params.get("sign");
		}
		boolean isSign = false;
		try {
			isSign = SignUtils.rsaCheckContent(publicCertFileInputStream,
					params, sign, DirectPayConfig.DEFAULT_CHARSET);
		} catch (Exception e) {
			throw new RuntimeException("验证签名失败，请检查银盛公钥证书文件是否存在");
		}

		return isSign;
	}

}
